<?php
/**
 * Created by PhpStorm.
 * User: Jiawei
 * Date: 2017/7/29
 * Time: 13:54
 */

namespace App\Lib\WeApp\Api;


use App\Wen\Utils\Settings;
use Illuminate\Support\Facades\Cache;

class TemplateMsg extends BaseApi
{

	public function getListFromLib($offset,$count){
		$url = ApiUrl::TEMPLATE_MSG_LIST_FORM_LIB;
		$param = array(
			'offset'=>$offset,
			'count'=>$count,
		);
		return $this->sendRequestWithToken($url,$param);
	}

	public function getTempFromLib($id){
		$url = ApiUrl::TEMPLATE_MSG_GET_FORM_LIB;
		$param = array(
			'id'=>$id,
		);
		return $this->sendRequestWithToken($url,$param);
	}

	public function add($id,$keyword_id_array){
		$url = ApiUrl::TEMPLATE_MSG_ADD;
		$param = array(
			'id'=>$id,
			'keyword_id_list'=>$keyword_id_array,
		);
		return $this->sendRequestWithToken($url,$param);
	}

	public function getList($offset,$count){
		$url = ApiUrl::TEMPLATE_MSG_LIST;
		$param = array(
			'offset'=>$offset,
			'count'=>$count,
		);
		return $this->sendRequestWithToken($url,$param);
	}

	public function del($template_id){
		$url = ApiUrl::TEMPLATE_MSG_DEL;
		$param = array(
			'template_id'=>$template_id,
		);
		return $this->sendRequestWithToken($url,$param);
	}

	public function subscribeMessage($touser, $template_id, $data, $mini_path){
	    if(_empty_($touser) || _empty_($template_id) || _empty_($data) || _empty_($mini_path)){
	        return false;
        }

        $url = ApiUrl::SUBSCRIBE_MSG_SEND;
        $param = array(
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page' => $mini_path,
            'miniprogram_state' => 'formal',
            'lang'=> 'zh_CN',
            'data'=>$data,
        );

        return $this->sendRequestWithToken($url,$param);
    }

	public function send($touser,$template_id,$data, $mini_path, $url = ''){
		$api_url = ApiUrl::TEMPLATE_MSG_SEND;
		$param = array(
			'touser'=>$touser,
			'template_id'=>$template_id,
			'client_msg_id'=> 'MSG_'.time(),
			'data'=>$data,
		);
		if(!_empty_($url)){
            $param['url'] = $url;
        }
		if(!_empty_($mini_path)){
            $param['miniprogram'] = [
                'appid' => Settings::get('app_id'),
                'pagepath' => $mini_path
            ];
        }
		return $this->sendRequestWithToken($api_url,$param, true);
	}

}
